package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.seckill.SecKillFeignClient;
import com.atguigu.gmall.feign.user.UserFeignClient;
import com.atguigu.gmall.model.activity.SeckillGoods;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.user.UserAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2024/10/18 4:14
 */
@Controller
public class SecKillController {
    @Autowired
    SecKillFeignClient secKillFeignClient;
    @Autowired
    UserFeignClient userFeignClient;
    @GetMapping("/seckill.html")
    public String secKillPage(Model model){
        //远程调用seckill页面
        Result<List<SeckillGoods>> secKillList = secKillFeignClient.getSecKillList();
        model.addAttribute("list",secKillList.getData());
        return "seckill/index";
    }
    @GetMapping("/seckill/{skuId}.html")
    public String secKillDetail(Model model,@PathVariable("skuId") Long skuId){
        //远程调用seckill商品详情
        Result<SeckillGoods> item = secKillFeignClient.getSecKillDetails(skuId);
        model.addAttribute("item",item.getData());
        return "seckill/item";
    }
    //跳转到排队页
    //seckill/queue.html?skuId=39&skuIdStr=4cb7065117ec83b30b7fe92752aec0f4
    @GetMapping("/seckill/queue.html")
    public String queuePage(@RequestParam("skuId") Long skuId
            ,@RequestParam("skuIdStr") String skuIdStr,Model model){
        model.addAttribute("skuId",skuId);
        model.addAttribute("skuIdStr",skuIdStr);
        return "seckill/queue";
    }
    @GetMapping("/seckill/trade.html")
    public String tradePage(@RequestParam("skuId") Long skuId,Model model){
        Result<OrderInfo> info = secKillFeignClient.getSecKillOrderInfoFromRedis(skuId);
        model.addAttribute("detailArrayList",info.getData().getOrderDetailList());
        model.addAttribute("totalNum","1");
        model.addAttribute("totalAmount",info.getData().getTotalAmount());
        Result<List<UserAddress>> userAddressList = userFeignClient.getUserAddressList();
        model.addAttribute("userAddressList",userAddressList.getData());
        return "seckill/trade";
    }
}
